//
//  YeeCollectionRootVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/14.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeCollectionRootVC.h"
#import "XRCommonSegMentView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "YeeCollectionVC.h"
#import "YeeAdvCollectionVC.h"
@interface YeeCollectionRootVC ()<XRCommonSegMentViewdelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
    XRCommonSegMentView  *_segmentView;
    UIScrollView          *_contentView;
    NSInteger             _currentIndex;//用于记录当前试图索引
    UIPanGestureRecognizer *_fakePanGestureRecognizer;
}
@end

@implementation YeeCollectionRootVC

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self addSegMentView];
    [self addContentView];
}
-(void)addNavbarView
{
    [super addNavbarView];
    self.title=@"收藏";
    [self configNavigationBarRightTitle:@"编辑"];
}
-(void)configOwnViews
{
    
    
}
-(void)bindViewModel
{
    
    
}
-(void)addSegMentView
{
    _segmentView=[[XRCommonSegMentView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 40) titleArray:@[@"商品",@"店铺"]];
    _segmentView.delegate=self;
    [self.view addSubview:_segmentView];
}
-(void)addContentView
{
    _contentView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, kMainScreenWidth, kMainScreenHeight-kNavBarHeight-kNavStausHeight-40)];
    _contentView.showsVerticalScrollIndicator=NO;
    _contentView.showsHorizontalScrollIndicator=NO;
    _contentView.delegate=self;
    _contentView.pagingEnabled=YES;
    _contentView.bounces=NO;
    _contentView.contentSize=CGSizeMake(kMainScreenWidth*2, 0);
    
    
    YeeAdvCollectionVC  *haveAnswerVC=[[YeeAdvCollectionVC alloc] init];
    [haveAnswerVC.view setFrame:CGRectMake(0, 0, kMainScreenWidth, _contentView.frame.size.height)];
    [_contentView addSubview:haveAnswerVC.view];
    
    YeeCollectionVC  *NohaveAnswerVC=[[YeeCollectionVC alloc] init];
    [NohaveAnswerVC.view setFrame:CGRectMake(kMainScreenWidth, 0, kMainScreenWidth, kMainScreenHeight-kNavBarHeight-kNavStausHeight-40)];
    [_contentView addSubview:NohaveAnswerVC.view];
    [self.view addSubview:_contentView];
    [self addChildViewController:haveAnswerVC];
    [self addChildViewController:NohaveAnswerVC];
    
    
    //下面一段代码将处理手势
    __block UIScrollView *scrollView = nil;
    [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIScrollView class]])
        {
            scrollView = (UIScrollView *)obj;
        }
    }];
    if(scrollView)
    {
        _fakePanGestureRecognizer = [UIPanGestureRecognizer new];
        _fakePanGestureRecognizer.delegate = self;
        [scrollView addGestureRecognizer:_fakePanGestureRecognizer];
        [scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.fd_fullscreenPopGestureRecognizer];
        [scrollView.panGestureRecognizer requireGestureRecognizerToFail:_fakePanGestureRecognizer];
        [_fakePanGestureRecognizer requireGestureRecognizerToFail:self.navigationController.fd_fullscreenPopGestureRecognizer];
    }
    
    
}
#pragma mark -- UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    
    if (translation.x <= 0)
    {
        return (_currentIndex == 1);
    }else
    {
        return (_currentIndex ==0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger  contentOffsetindex=scrollView.contentOffset.x/kMainScreenWidth;
    [_segmentView setLineViewFrameWithClickIndex:contentOffsetindex];
    _currentIndex=contentOffsetindex;
    self.fd_interactivePopDisabled=contentOffsetindex!=0;
}
-(void)XRCommonSegMentView:(XRCommonSegMentView *) segmentView ClickatIndex:(NSInteger)index
{
    [_contentView setContentOffset:CGPointMake(index*kMainScreenWidth, 0) animated:NO];
    _currentIndex=index;
    self.fd_interactivePopDisabled=index!=0;
}
- (void)navigationBarRightButtonTapped:(UIButton *)sender
{
    
    [self.view showNBErrorMessage:@"点击按钮了..."];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}


@end
